package cn.fzc.yipai.service.impl;

import cn.fzc.yipai.entity.*;
import cn.fzc.yipai.mapper.GoodsKillRecordMapper;
import cn.fzc.yipai.service.GoodsKillRecordService;
import cn.fzc.yipai.utils.HttpUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyh
 * @since 2021-07-20
 */
@Service
public class GoodsKillRecordServiceImpl extends ServiceImpl<GoodsKillRecordMapper, GoodsKillRecord> implements GoodsKillRecordService {

    public static final String[] detailList = new String[]{"https://www.yihui-xh.xyz/yip/otc/advertise/getgoodssellerlist/1/0/-1/1/500",
            "https://www.yihui-xh.xyz/yip/otc/advertise/getgoodssellerlist/2/0/-1/1/500",
            "https://www.yihui-xh.xyz/yip/otc/advertise/getgoodssellerlist/3/0/-1/1/500"};


    @Autowired
    private HttpUtils httpUtils;

    @Override
    public void insertRecord() {
        for (String url : detailList) {
            List<GoodsKillRecord> recordList = new ArrayList<>();
            List<NewGoodsInfo> goodsInfos = doGetRecord(url);
            if (CollectionUtils.isNotEmpty(goodsInfos)) {
                for (NewGoodsInfo goodsInfo : goodsInfos) {
                    GoodsInfo goodsInfoVO = goodsInfo.getGoodsInfoVO();
                    GoodsSeller goodsSellerVO = goodsInfo.getGoodsSellerVO();
                    GoodsKillRecord record = new GoodsKillRecord();
                    record.setGoodsId(goodsInfoVO.getId());
                    record.setStoresId(goodsInfoVO.getStoresId());
                    record.setTitle(goodsInfoVO.getTitle());
                    record.setBasePrice(goodsInfoVO.getBasePrice());
                    record.setQuotePrice(goodsInfoVO.getQuotePrice());
                    record.setCoverUrl(goodsInfoVO.getCoverUrl());
                    record.setDetailCoverUrls(goodsInfoVO.getDetailCoverUrls());
                    record.setCreatedTime(goodsInfoVO.getCreatedTime());
                    record.setLastUpdatedTime(goodsInfoVO.getLastUpdatedTime());
                    record.setMName(goodsSellerVO.getMName());
                    record.setUserId(goodsSellerVO.getUserId());
                    record.setOrginalPrice(goodsSellerVO.getOrginalPrice());
                    record.setOrderId(goodsSellerVO.getOrderId());
                    record.setStatus(goodsSellerVO.getStatus());
                    record.setGoodsStatus(goodsSellerVO.getGoodsStatus());
                    recordList.add(record);
                }
            }
            if (CollectionUtils.isNotEmpty(recordList)) {
                this.saveBatch(recordList);
            }
        }
    }


    private List<NewGoodsInfo> doGetRecord(String url) {
        String result = httpUtils.executeGet(url, "123123123123123123");
        if (StringUtils.isNotBlank(result)) {
            Response<PageInfo<NewGoodsInfo>> pageInfoResponse = JSON.parseObject(result, new TypeReference<Response<PageInfo<NewGoodsInfo>>>() {
            });
            return pageInfoResponse.getResult().getList();
        }
        return Lists.newArrayList();
    }


}
